using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using PSE.Deployment.SetupBase.Parameter.Enums;

namespace PSE.Deployment.SetupBase.Commands
{
    public class ServicePackCommand : ICommand
    {
        private const string HideWizardOption = "/hidewizard";
        private const string HideResultOption = "/hideresult";
        private const string FileNameOption = "/filename";

        private bool ValidateArguments(string[] args, out string[] returnArgs)
        {
            bool isvalid = true;
            returnArgs = null;

            bool showWizard = true;
            bool showResult = true;
            string filename = String.Empty;

            for (int i = 1; i < args.Length; i++)
            {
                string[] arg;

                if (args[i].StartsWith("/"))
                {
                    arg = args[i].Split('=');

                    switch (arg[0].Trim().ToLower())
                    {
                        case HideWizardOption:
                            showWizard = false;
                            break;
                        case HideResultOption:
                            showResult = false;
                            break;
                        case FileNameOption:
                            if (arg.Length == 2)
                            {
                                filename = arg[1].Trim().ToLower();

                                if (!File.Exists(filename.Replace("'", "").Replace("\"", "")))
                                {
                                    Messages.FileDoesNotExistsError();
                                    isvalid = false;
                                }
                            }
                            else
                            {
                                Messages.InvalidOptionError(FileNameOption);
                                isvalid = false;
                            }
                            break;
                    }
                }
                else
                {
                    if (String.IsNullOrEmpty(filename))
                    {
                        filename = args[i];
                    }
                    else
                    {
                        Messages.WriteError("Invalid file name.");
                        isvalid = false;
                    }
                }
            }

            if (String.IsNullOrEmpty(filename))
            {
                Messages.NoFileSpecifiedError();
                return false;
            }

            returnArgs = new string[] { filename, showWizard.ToString(), showResult.ToString() };

            return isvalid;
        }

        #region ICommand Members

        public int Execute(string[] args)
        {
            string[] returnArgs;
            int executed = 0;

            if (ValidateArguments(args, out returnArgs))
            {
                string filename = returnArgs[0];
                string showWizard = returnArgs[1];
                string showResult = returnArgs[2];

                try
                {
                    if (SetupMainFactory.GetInstance().Parameter.IsInstalled)
                    {
                        SetupMainFactory.GetInstance().LoadConfiguration(filename);
                        SetupMainFactory.GetInstance().Parameter.SetupMode = SetupMode.ServicePack;
                        SetupMainFactory.GetInstance().Parameter.ShowWizard = Convert.ToBoolean(showWizard);
                        SetupMainFactory.GetInstance().Parameter.ShowResult = Convert.ToBoolean(showResult);

                        SetupApplication application = new SetupApplication();
                        application.CheckCurrentProcess = false;
                        executed = application.Run();
                    }
                    else
                        Messages.SystemNotInstalled();
                }
                catch (Exception ex)
                {
                    executed = -1;
                    Messages.WriteFailMessage(ex);
                }
            }

            return executed;
        }

        #endregion
    }
}